Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms


Namespace Phoenix.Tools.Backup
	Public Class DatabasePage : Inherits Phoenix.Tools.WizardPageBase
		#Region "Form Controls"
		Private lblDescription As System.Windows.Forms.Label
		Private lblServer As System.Windows.Forms.Label
		Private WithEvents listServer As System.Windows.Forms.ComboBox
		Private btnWindowsAuth As System.Windows.Forms.RadioButton
		Private WithEvents btnSqlAuth As System.Windows.Forms.RadioButton
		Private WithEvents btnRefreshServers As System.Windows.Forms.Button
		Private lblUsername As System.Windows.Forms.Label
		Private lblPassword As System.Windows.Forms.Label
		Private lblDatabase As System.Windows.Forms.Label
		Private WithEvents listDatabases As System.Windows.Forms.ComboBox
		Private WithEvents btnRefreshDatabases As System.Windows.Forms.Button
		Private components As System.ComponentModel.IContainer = Nothing
		#End Region

		#Region "Form Data"
		Private mServersLoaded As Boolean = False
		Private mDatabasesLoaded As Boolean = False
		Private txtUsername As System.Windows.Forms.TextBox
		Private txtPassword As System.Windows.Forms.TextBox
		#End Region

		#Region "Contructors/Finalizators"
		Public Sub New()
			' This call is required by the Windows Form Designer.
			InitializeComponent()

			' TODO: Add any initialization after the InitializeComponent call
		End Sub

		''' <summary>
		''' Clean up any resources being used.
		''' </summary>
		Protected Overrides Overloads Sub Dispose(ByVal disposing As Boolean)
			If disposing Then
				If Not components Is Nothing Then
					components.Dispose()
				End If
			End If
			MyBase.Dispose(disposing)
		End Sub

		#End Region

		#Region "Designer generated code"
		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(DatabasePage))
			Me.lblDescription = New System.Windows.Forms.Label()
			Me.lblServer = New System.Windows.Forms.Label()
			Me.listServer = New System.Windows.Forms.ComboBox()
			Me.btnWindowsAuth = New System.Windows.Forms.RadioButton()
			Me.btnSqlAuth = New System.Windows.Forms.RadioButton()
			Me.btnRefreshServers = New System.Windows.Forms.Button()
			Me.lblUsername = New System.Windows.Forms.Label()
			Me.lblPassword = New System.Windows.Forms.Label()
			Me.txtUsername = New System.Windows.Forms.TextBox()
			Me.txtPassword = New System.Windows.Forms.TextBox()
			Me.lblDatabase = New System.Windows.Forms.Label()
			Me.listDatabases = New System.Windows.Forms.ComboBox()
			Me.btnRefreshDatabases = New System.Windows.Forms.Button()
			Me.SuspendLayout()
			' 
			' lblDescription
			' 
			Me.lblDescription.Location = New System.Drawing.Point(24, 8)
			Me.lblDescription.Name = "lblDescription"
			Me.lblDescription.Size = New System.Drawing.Size(456, 43)
			Me.lblDescription.TabIndex = 0
			Me.lblDescription.Text = "To connect to Microsoft SQL Server, you must specify the server, user name, and p" & "assword."
			' 
			' lblServer
			' 
			Me.lblServer.Location = New System.Drawing.Point(40, 60)
			Me.lblServer.Name = "lblServer"
			Me.lblServer.Size = New System.Drawing.Size(48, 16)
			Me.lblServer.TabIndex = 1
			Me.lblServer.Text = "&Server:"
			' 
			' listServer
			' 
			Me.listServer.Location = New System.Drawing.Point(128, 56)
			Me.listServer.Name = "listServer"
			Me.listServer.Size = New System.Drawing.Size(232, 21)
			Me.listServer.TabIndex = 0
			Me.listServer.Text = "(local)"
'			Me.listServer.DropDown += New System.EventHandler(Me.listServer_DropDown);
'			Me.listServer.TextChanged += New System.EventHandler(Me.listServer_TextChanged);
'			Me.listServer.SelectedIndexChanged += New System.EventHandler(Me.listServer_SelectedIndexChanged);
			' 
			' btnWindowsAuth
			' 
			Me.btnWindowsAuth.Checked = True
			Me.btnWindowsAuth.FlatStyle = System.Windows.Forms.FlatStyle.System
			Me.btnWindowsAuth.Location = New System.Drawing.Point(56, 96)
			Me.btnWindowsAuth.Name = "btnWindowsAuth"
			Me.btnWindowsAuth.Size = New System.Drawing.Size(184, 24)
			Me.btnWindowsAuth.TabIndex = 2
			Me.btnWindowsAuth.TabStop = True
			Me.btnWindowsAuth.Text = "Use &Windows Authentication"
			' 
			' btnSqlAuth
			' 
			Me.btnSqlAuth.FlatStyle = System.Windows.Forms.FlatStyle.System
			Me.btnSqlAuth.Location = New System.Drawing.Point(56, 128)
			Me.btnSqlAuth.Name = "btnSqlAuth"
			Me.btnSqlAuth.Size = New System.Drawing.Size(184, 24)
			Me.btnSqlAuth.TabIndex = 3
			Me.btnSqlAuth.Text = "Use S&QL Authentication"
'			Me.btnSqlAuth.CheckedChanged += New System.EventHandler(Me.btnSqlAuth_CheckedChanged);
			' 
			' btnRefreshServers
			' 
			Me.btnRefreshServers.FlatStyle = System.Windows.Forms.FlatStyle.System
			Me.btnRefreshServers.Location = New System.Drawing.Point(368, 56)
			Me.btnRefreshServers.Name = "btnRefreshServers"
			Me.btnRefreshServers.TabIndex = 1
			Me.btnRefreshServers.Text = "&Refresh"
'			Me.btnRefreshServers.Click += New System.EventHandler(Me.btnRefreshServers_Click);
			' 
			' lblUsername
			' 
			Me.lblUsername.Location = New System.Drawing.Point(82, 162)
			Me.lblUsername.Name = "lblUsername"
			Me.lblUsername.Size = New System.Drawing.Size(70, 14)
			Me.lblUsername.TabIndex = 5
			Me.lblUsername.Text = "&Username:"
			' 
			' lblPassword
			' 
			Me.lblPassword.Location = New System.Drawing.Point(82, 187)
			Me.lblPassword.Name = "lblPassword"
			Me.lblPassword.Size = New System.Drawing.Size(70, 16)
			Me.lblPassword.TabIndex = 6
			Me.lblPassword.Text = "&Password:"
			' 
			' txtUsername
			' 
			Me.txtUsername.Enabled = False
			Me.txtUsername.Location = New System.Drawing.Point(168, 160)
			Me.txtUsername.Name = "txtUsername"
			Me.txtUsername.Size = New System.Drawing.Size(192, 20)
			Me.txtUsername.TabIndex = 3
			Me.txtUsername.Text = ""
			' 
			' txtPassword
			' 
			Me.txtPassword.Enabled = False
			Me.txtPassword.Location = New System.Drawing.Point(168, 185)
			Me.txtPassword.Name = "txtPassword"
			Me.txtPassword.PasswordChar = "*"c
			Me.txtPassword.Size = New System.Drawing.Size(192, 20)
			Me.txtPassword.TabIndex = 4
			Me.txtPassword.Text = ""
			' 
			' lblDatabase
			' 
			Me.lblDatabase.Location = New System.Drawing.Point(56, 218)
			Me.lblDatabase.Name = "lblDatabase"
			Me.lblDatabase.Size = New System.Drawing.Size(64, 16)
			Me.lblDatabase.TabIndex = 7
			Me.lblDatabase.Text = "&Database:"
			' 
			' listDatabases
			' 
			Me.listDatabases.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
			Me.listDatabases.Items.AddRange(New Object() { "<select database>"})
			Me.listDatabases.Location = New System.Drawing.Point(128, 216)
			Me.listDatabases.Name = "listDatabases"
			Me.listDatabases.Size = New System.Drawing.Size(232, 21)
			Me.listDatabases.TabIndex = 8
'			Me.listDatabases.DropDown += New System.EventHandler(Me.listDatabases_DropDown);
'			Me.listDatabases.SelectedValueChanged += New System.EventHandler(Me.listDatabases_SelectedValueChanged);
			' 
			' btnRefreshDatabases
			' 
			Me.btnRefreshDatabases.FlatStyle = System.Windows.Forms.FlatStyle.System
			Me.btnRefreshDatabases.Location = New System.Drawing.Point(368, 216)
			Me.btnRefreshDatabases.Name = "btnRefreshDatabases"
			Me.btnRefreshDatabases.TabIndex = 9
			Me.btnRefreshDatabases.Text = "Re&fresh"
'			Me.btnRefreshDatabases.Click += New System.EventHandler(Me.btnRefreshDatabases_Click);
			' 
			' DatabasePage
			' 
			Me.Controls.Add(Me.btnRefreshDatabases)
			Me.Controls.Add(Me.listDatabases)
			Me.Controls.Add(Me.lblDatabase)
			Me.Controls.Add(Me.txtPassword)
			Me.Controls.Add(Me.txtUsername)
			Me.Controls.Add(Me.lblPassword)
			Me.Controls.Add(Me.lblUsername)
			Me.Controls.Add(Me.btnRefreshServers)
			Me.Controls.Add(Me.btnSqlAuth)
			Me.Controls.Add(Me.btnWindowsAuth)
			Me.Controls.Add(Me.listServer)
			Me.Controls.Add(Me.lblServer)
			Me.Controls.Add(Me.lblDescription)
			Me.Description = "Choose an Agronaut database to back up or restore."
			Me.HeaderImage = (CType(resources.GetObject("$this.HeaderImage"), System.Drawing.Image))
			Me.Name = "DatabasePage"
			Me.Size = New System.Drawing.Size(875, 635)
			Me.Title = "Choose a Database"
'			Me.Next += New Phoenix.Tools.WizardForm.WizardNextEventHandler(Me.DatabasePage_Next);
'			Me.Load += New System.EventHandler(Me.DatabasePage_Load);
			Me.ResumeLayout(False)

		End Sub
		#End Region

		#Region "Form Properries"
		Public Manager As OperationManager


		#End Region

		#Region "Form Routines"
		Private Sub ResetServerList()
			mServersLoaded = False
			listServer.Items.Clear()
			listServer.Items.Add("(local)")
			listServer.SelectedIndex = 0
		End Sub

		Private Sub ResetDatabaseList()
			mDatabasesLoaded = False
			listDatabases.Items.Clear()
			listDatabases.Items.Add("<select database>")
			listDatabases.SelectedIndex = 0
		End Sub


		Private Sub UpdateData()
			If listDatabases.SelectedIndex = 0 Then
				Manager.DBDatabase = Nothing
			Else
				Manager.DBDatabase = CStr(listDatabases.SelectedItem)
			End If

			Manager.DBServer = listServer.Text
			Manager.DBWindowAuthentication = btnWindowsAuth.Checked
			Manager.DBUsername = txtUsername.Text
			Manager.DBPassword = txtPassword.Text
		End Sub

		Private Sub LoadDatabaseList()
			Dim selectedDb As String = CStr(listDatabases.Text)

			ResetDatabaseList()
			UpdateData()

			Cursor.Current = Cursors.WaitCursor
			Try
				listDatabases.Items.AddRange(Manager.ListDatabases())

				Dim index As Integer = listDatabases.Items.IndexOf(selectedDb)
				If index = -1 Then
					index = 0
				End If
				listDatabases.SelectedIndex = index
			Catch e As Exception
				MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Finally
				Cursor.Current = Cursors.Default
			End Try
		End Sub

		Private Sub LoadServerList()
			UpdateData()
			Try
				Dim selectedServer As String = CStr(listServer.Text)

				mServersLoaded = False
				Cursor.Current = Cursors.WaitCursor
				Try
					For Each name As String In Manager.ListServers()
						If name = "(local)" Then
							GoTo Continue1
						End If
						listServer.Items.Add(name)
						Continue1:
					Next name

					Dim index As Integer = listServer.Items.IndexOf(selectedServer)
					If index<>-1 Then
						listServer.SelectedIndex = index
					End If
				Finally
					Cursor.Current = Cursors.Default
				End Try

				mServersLoaded = True
			Catch e As Exception
				MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End Sub
		#End Region

		#Region "Form/Controls Events"
		Private Sub listServer_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles listServer.DropDown
			If (Not mServersLoaded) Then
				LoadServerList()
			End If
		End Sub

		Private Sub btnRefreshServers_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRefreshServers.Click
			LoadServerList()
		End Sub

		Private Sub btnSqlAuth_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSqlAuth.CheckedChanged
			txtUsername.Enabled = btnSqlAuth.Checked
			txtPassword.Enabled = btnSqlAuth.Checked
		End Sub

		Private Sub DatabasePage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
			ResetDatabaseList()
			ResetServerList()
		End Sub

		Private Sub listDatabases_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles listDatabases.DropDown
			If (Not mDatabasesLoaded) Then
				LoadDatabaseList()
			End If
		End Sub

		Private Sub btnRefreshDatabases_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRefreshDatabases.Click
			LoadDatabaseList()
		End Sub

		Private Sub listServer_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles listServer.SelectedIndexChanged
			ResetDatabaseList()
		End Sub

		Private Sub listServer_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles listServer.TextChanged
			ResetDatabaseList()
		End Sub

        Private Sub DatabasePage_Next(ByVal sender As Object, ByVal e As Phoenix.Tools.WizardForm.EventNextArgs) Handles MyBase.NextEvent
            UpdateData()
        End Sub

        Private Sub listDatabases_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles listDatabases.SelectedValueChanged
            If Not WizardPageParent Is Nothing Then
                If listDatabases.SelectedIndex = 0 Then
                    WizardPageParent.WizardButtons = WizardForm.TWizardsButtons.Previous Or WizardForm.TWizardsButtons.Cancel
                Else
                    WizardPageParent.WizardButtons = WizardForm.TWizardsButtons.AllShort
                End If
            End If
        End Sub

#End Region

		Protected Friend Overrides Sub OnPageShown()
			listDatabases_SelectedValueChanged(listDatabases, Nothing)
		End Sub

	End Class
End Namespace

